<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>合同创建</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/font.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/static/js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">合同</a>
        <a>
          <cite>审核</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="x-body">
      <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so" onsubmit="return false;">
          <input type="text" name="keywords"  placeholder="合同名称" autocomplete="off" class="layui-input">
          <div class="layui-input-inline">
            <select name="categoryId">
              <option>规则分类</option>
              <option>文章</option>
              <option>会员</option>
              <option>权限</option>
            </select>
          </div>
          <div class="layui-input-inline">
            <select name="statusId">
              <option>规则分类</option>
              <option>文章</option>
              <option>会员</option>
              <option>权限</option>
            </select>
          </div>
          <input class="layui-input" placeholder="开始日" name="startTime" id="start">
          <input class="layui-input" placeholder="截止日" name="endTime" id="end">
          <button class="layui-btn" id="search" ><i class="layui-icon">&#xe615;</i></button>
        </form>
      </div>
      <xblock class="do-table">
        <button class="layui-btn layui-btn-danger" data-type="deleteAll"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn" onclick="x_admin_show('合同创建','${pageContext.request.contextPath}/contract/add/page')"><i class="layui-icon"></i>合同创建</button>
      </xblock>
      <table id="data" lay-filter="contract"></table>


    </div>
    <script>
      var from;
      layui.use(['form','layer'], function(){
        $ = layui.jquery;
        form = layui.form
        var layer = layui.layer;
        // 获取种类列表
        $.ajax({
          type:"post",
          url:"${pageContext.request.contextPath}/contract/category/data",
          dataType: "json",
          success:function (res) {
            if (res.code === 200){
              $("select[name=categoryId]").empty();
              //添加不限选项
              var option = $('<option value="">'+'不限'+'</option>')
              // 追加
              $("select[name=categoryId]").append(option);
              $(res.data).each(function () {
                // 动态创建
                 option = $('<option value="'+this.id+'">'+this.category+'</option>')
                // 追加
                $("select[name=categoryId]").append(option);
              })
              // 重新渲染表单
              form.render('select');
            }
          }
        });

        // 获取状态列表
        $.ajax({
          type:"post",
          url:"${pageContext.request.contextPath}/contract/status/data",
          dataType: "json",
          success:function (res) {
            if (res.code === 200){
              $("select[name=statusId]").empty();
              //添加不限选项
              var option = $('<option value="">'+'不限'+'</option>')
              // 追加
              $("select[name=statusId]").append(option);
              $(res.data).each(function () {
                // 动态创建
                var option = $('<option value="'+this.id+'">'+this.statu+'</option>')
                // 追加
                $("select[name=statusId]").append(option);
              })
              // 重新渲染表单
              form.render('select');
            }
          }
        });

      });


      var table;
      layui.use(['laydate','table'], function(){

        var laydate = layui.laydate;
        table = layui.table;
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });
        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });

        table.render({
          elem: '#data'
          ,height: 500
          ,url: '${pageContext.request.contextPath}/contract/list/data' //数据接口
          ,method: "post"
          ,page: true //开启分页
          ,limit:5
          ,limits:[5,10,15]
          ,cols: [[ //表头
            {type:'checkbox', fixed: 'left'}
            ,{field: 'id', title: 'ID', width:'5%', sort: true}
            ,{field: 'contractName', title: '合同名称', width:'20%'}
            ,{field: 'categoryId', title: '合同类型', width:'10%' ,templet:function (d) {
                        return d.contractCategory.category;
              }}
            ,{field: 'amount', title: '合同金额', width:'10%'}
            ,{field: 'statusId', title: '状态', width:'10%', templet:function (d) {
                  return d.contractStatus.statu;
              }}
            ,{field: 'lastOpdate', title: '最后操作时间', width:'15%'}
            ,{field: 'emplId', title: '待处理人', width: '10%', templet:function (d) {
                return d.empl.emplRealName;
              }}
            ,{fixed: 'right',title: '操作', width:'15%', align:'center', toolbar: '#do'}
          ]]
        });
        //工具条事件
        table.on('tool(contract)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
          var data = obj.data; //获得当前行数据
          var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
          var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

          if(layEvent === 'pass'){ //通过合同
            if (data.statusId !== 3){
              layer.msg('此合同不在审核范围！', {icon: 2}, {time: 500});
            }
            else{

            // 发送ajax改变状态
            $.ajax({
              type:"post",
              url:"${pageContext.request.contextPath}/contract/audit/do",
              data: {id:data.id,statusId:4},
              dataType: "json",
              success:function (res) {
                if (res.code === 200){
                  layer.msg(res.msg, {icon: 6}, {time: 500});
                  table.reload('data');
                }
              }
            });
            }
          } else if(layEvent === 'back'){ //打回合同

            if (data.statusId !== 3){
              layer.msg('此合同不在审核范围！', {icon: 2}, {time: 500});
            }
            else{

            layer.confirm('真的打回此合同么', function(index){

              layer.close(index);
              //向服务端发送删除指令
              $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/contract/audit/do",
                data: {id:data.id,statusId:1},
                dataType: "json",
                success:function (res) {
                  if (res.code === 200){
                    layer.msg(res.msg, {icon: 6}, {time: 500});
                    table.reload('data');
                  }
                }
              });

            });
            }
          } else if(layEvent === 'audit'){ //审核
            if (data.statusId !== 3){
              layer.msg('此合同不在审核范围！', {icon: 2}, {time: 500});
            }
            else{
            //do something
            x_admin_show('审核合同','${pageContext.request.contextPath}/contract/audit/page?id='+data.id)
            }

        }else if(layEvent === 'LAYTABLE_TIPS'){
            layer.alert('Hi，头部工具栏扩展的右侧图标。');
          }
        });
        var $ = layui.$, active = {
          deleteAll: function(){
            var checkStatus = table.checkStatus('data')
                    ,data = checkStatus.data;
            if (data.length === 0){
              layer.msg('请至少选中一行数据，进行删除！');
              return;
            }
            layer.confirm('确认要删除吗？',function(index){
              //获取选中数据中的id并组装成一个数组
              var ids = [];
              $(data).each(function () {
                if (this.statusId === 2){
                  layer.msg('存在已提交合同，无法删除！', {icon: 2}, {time: 500});
                  return false;
                }
                ids.push(this.id);
              })
              // 发送请求到后端
              $.ajax({
                type:"post",
                url:"${pageContext.request.contextPath}/contract/delete",
                data: {ids: ids},
                dataType: "json",
                success:function (res) {
                  layer.msg(res.msg, {icon: 6},function () {
                    // 刷新父级的表格
                    table.reload('data');
                  });
                }
              });
            });
          }
        };

        $('.do-table .layui-btn-danger').on('click', function(){
          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
        });

        // 搜索按钮
        $("#search").click(function () {
          //获取对应的数据
          //获取状态id
          var statusId= $("select[name='statusId']").val();
          //获取种类id
          var categoryId= $("select[name='categoryId']").val();
          // 获取关键字
          var keywords = $("input[name='keywords']").val();
          // 获取开始时间
          var startTime = $("#start").val();
          // 获取结束时间
          var endTime = $("#end").val();
          // 组装总共的提交参数
          var param = {keywords: keywords, categoryId:categoryId,statusId:statusId,startTime: startTime ? startTime+" 00:00:00" : '', endTime: endTime ? endTime+" 23:59:59": ''}
          // 表格数据的重载
          table.reload("data",{where: param})
        });
      });

    </script>


    <script type="text/html" id="do">
      <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="audit">审核</a>
      <a class="layui-btn layui-btn-xs" lay-event="pass">通过</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="back">打回</a>
    </script>

  </body>

</html>